
package jp.ize.iwp.avm1.display;

// {{{ imports
import flash.MovieClip;
import flash.MovieClipLoader;
import jp.ize.iwp.avm1.events.EventDispatcher;
import jp.ize.iwp.avm1.events.Event;
import jp.ize.iwp.avm1.events.IOErrorEvent;
// }}}

class Loader extends EventDispatcher
{
	// {{{ properties

	public static var loaderName:String = "__loader";
	public static var loaderIdCount:Int = 0;

	public var contentLoaderInfo:Loader;
	public var content:MovieClip;

	// }}}
	// {{{ public function new() : Void

	public function new() : Void
	{
		super();
		contentLoaderInfo = this;
	}

	// }}}
	// {{{ public function load( url:String, ?parent:MovieClip ) : Void

	public function load( url:String, ?parent:MovieClip ) : Void
	{
		var mcl = new MovieClipLoader();
		if ( parent==null ) {
			parent = flash.Lib._root;
		}
		var name = loaderName + Std.string(loaderIdCount);
		loaderIdCount++;
		content = parent.createEmptyMovieClip( name, parent.getNextHighestDepth() );
		mcl.onLoadInit     = onLoadInit;
		mcl.onLoadStart    = onLoadStart;
		mcl.onLoadProgress = onLoadProgress;
		mcl.onLoadComplete = onLoadComplete;
		mcl.onLoadError    = onLoadError;
		mcl.loadClip( url, content );
	}

	// }}}
	// {{{ public function onLoadInit( target:MovieClip ) : Void

	public function onLoadInit( target:MovieClip ) : Void
	{
		var e = new Event(Event.COMPLETE,true,true);
		e.target = target;
		dispatchEvent(e);
	}

	// }}}
	// {{{ public function onLoadStart( target:MovieClip ) : Void

	public function onLoadStart( target:MovieClip ) : Void
	{
	}

	// }}}
	// {{{ public function onLoadProgress( target:MovieClip, loaded:Int, total:Int ) : Void

	public function onLoadProgress( target:MovieClip, loaded:Int, total:Int ) : Void
	{
	}

	// }}}
	// {{{ public function onLoadComplete( target:MovieClip ) : Void

	public function onLoadComplete( target:MovieClip ) : Void
	{
	}

	// }}}
	// {{{ public function onLoadError( target:MovieClip, err:String ) : Void

	public function onLoadError( target:MovieClip, err:String ) : Void
	{
		var e = new IOErrorEvent(IOErrorEvent.IO_ERROR,true,true,err);
		dispatchEvent(e);
	}

	// }}}
}
